<template>
  <div>
    <!-- 需求: 小选框都选中(手选), 全选自动选中 -->
    <span>全选:</span>
    <input type="checkbox" v-model="isAll" value="cc" />
    <button @click="btn">反选</button>
    <ul>
      <li v-for="item in arr" :key="item.name">
        <input type="checkbox" v-model="item.c" />
        <span>{{ item.name }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        {
          name: "猪八戒",
          c: false,
        },
        {
          name: "孙悟空",
          c: false,
        },
        {
          name: "唐僧",
          c: false,
        },
        {
          name: "白龙马",
          c: false,
        },
      ],
    };
  },
  computed: {
    isAll() {
      // 6. 统计小选框状态 ->  全选状态
      // every口诀: 查找数组里"不符合"条件, 直接原地返回false
      return this.arr.every((obj) => obj.c === true);
    },
  },
  methods: {
    btn() {
      this.arr.forEach((obj) => (obj.c = !obj.c));
    },
  },
  // watch: {
  //   isAll(newVal, oldVal) {
  //     if (oldVal === true) {
  //       newVal = this.arr.forEach((obj) => (obj.c = !obj.c));
  //     }
  //     console.log(newVal, oldVal);
  //   },
  // },
};
</script>